#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* fast = head, * slow = head;
        ListNode* prev = nullptr;
        if (head->next == nullptr) {
            return nullptr;
        }

        for (int i = 0; i < n - 1; i++) {
            fast = fast->next;
        }
        while (fast->next) {
            prev = slow;
            slow = slow->next;
            fast = fast->next;
        }
        if (prev) {
            prev->next = slow->next;
            delete slow;

        }
        else {
            ListNode* cur = head;
            head = head->next;
            delete cur;
        }
        return head;
    }
};